package com.tencent.iov.cowin.befsale.clue.controller.clue;

import com.alibaba.fastjson.JSON;
import com.tencent.iov.cowin.befsale.clue.controller.clue.assembler.ClueInitialAssembler;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.request.*;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.response.ClueInitialResponse;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.response.ThirdCreateClueInitialResponse;
import com.tencent.iov.cowin.befsale.clue.dao.http.iam.IamIncCenterClient;
import com.tencent.iov.cowin.befsale.clue.dao.http.iam.request.CenterPhoneRequest;
import com.tencent.iov.cowin.befsale.clue.dao.http.iam.response.CenterPhoneResponse;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueInitial;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueLog;
import com.tencent.iov.cowin.befsale.clue.service.clue.ClueInitialService;
import com.software.boot.common.entity.Response;
import com.software.boot.common.model.dao.PageRequest;
import com.software.boot.common.model.dao.PageResponse;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.request.*;
import com.tencent.iov.cowin.befsale.clue.service.clue.ClueLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Describe: 线索源信息表_Controller层
 * @author: whp
 * @created: 2021-12-21 16:16:13
 * @version: 1.0
 */
@RestController
@Slf4j
@Api(tags = "线索源信息表Controller")
@RequestMapping(value = "/v1/clueInitial", produces = MediaType.APPLICATION_JSON_VALUE)
public class ClueInitialController {

    @Autowired
    private ClueInitialService clueInitialService;
    @Autowired
    private IamIncCenterClient iamIncCenterClient;
    @Autowired
    private ClueLogService clueLogService;

    /**
     * 通过ID获取详细信息
     *
     * @param: id
     * @return: ClueInitial对象
     */
    @ApiOperation(value = "ClueInitial管理-通过ID获取详细信息接口", httpMethod = "GET", response = Response.class, notes = "get_detail_by_id")
    @RequestMapping(path = "/get_detail_by_id", method = RequestMethod.GET)
    public Response<ClueInitialResponse> getDetailById(@ApiParam(required = true, value = "对象ID", defaultValue = "1") @RequestParam("id") Integer id) {
        return Response.success(ClueInitialAssembler.transformPOToResponseVo(clueInitialService.getDetailById(id), ClueInitialResponse.class));
    }

    /**
     * 单条数据插入ClueInitial
     * add by zly
     * 企微 总部顾问或者经销商顾问在企微页面新增时调用，新增的线索源会立即进行线索下发。
     * @param request
     * @return 插入的条数
     */
    @ApiOperation(value = "ClueInitial管理-添加信息接口", httpMethod = "POST", response = Response.class, notes = "add")
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    public Response<Long> add(@Valid @RequestBody AddClueInitialRequest request) {
        return Response.success(clueInitialService.insert(ClueInitialAssembler.transformVOToPO(ClueInitialAssembler.initAddClueInitialRequest(request), ClueInitial.class)));
    }

    /**
     * 批量插入ClueInitial
     *
     * @param requests
     * @return 批量插入的条数
     */
    @ApiOperation(value = "ClueInitial管理-批量插入ClueInitial接口", httpMethod = "POST", response = Response.class, notes = "add_batch")
    @RequestMapping(path = "/add_batch", method = RequestMethod.POST)
    public Response<Integer> addBatch(@Valid @RequestBody List<QueryClueInitialRequest> requests) {
        return Response.success(clueInitialService.insertBatch(ClueInitialAssembler.transformPOToResponseVoList(requests, ClueInitial.class)));
    }

    /**
     * 通过ID修改信息接口
     *
     * @param: request
     * @return: 修改的条数
     */
    @ApiOperation(value = "ClueInitial管理-修改信息接口", httpMethod = "POST", response = Response.class, notes = "update")
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public Response<Integer> update(@Valid @RequestBody UpdateClueInitialRequest request) {
        return Response.success(clueInitialService.update(ClueInitialAssembler.transformVOToPO(request, ClueInitial.class)));
    }

    /**
     * 通过ID删除数据
     *
     * @param request
     * @return 删除的条数
     */
    @ApiOperation(value = "ClueInitial管理-通过ID删除接口", httpMethod = "POST", response = Response.class, notes = "delete")
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    public Response<Integer> delete(@Valid @RequestBody DeleteClueInitialRequest request) {
        return Response.success(clueInitialService.delete(ClueInitialAssembler.transformVOToPO(request, ClueInitial.class)));
    }

    /**
     * 根据条件查询对象list列表
     *
     * @param request
     * @return 查询结果
     */
    @ApiOperation(value = "ClueInitial管理-根据条件查询对象list列表接口", httpMethod = "POST", response = Response.class, notes = "query_list_by_param")
    @RequestMapping(path = "/get_list_by_param", method = RequestMethod.POST)
    public Response<List<ClueInitialResponse>> getListByParam(@RequestBody QueryClueInitialRequest request) {
        List<ClueInitial> responseList = clueInitialService.getListByParam(ClueInitialAssembler.transformVOToPO(request, ClueInitial.class));
        return Response.success(ClueInitialAssembler.transformPOToResponseVoList(responseList, ClueInitialResponse.class));
    }

    /**
     * 根据条件查询对象Page记录
     *
     * @param request
     * @return 查询结果
     */
    @ApiOperation(value = "ClueInitial管理-根据条件查询对象Page记录接口", httpMethod = "POST", response = Response.class, notes = "get_page_by_param")
    @RequestMapping(value = "/get_page_by_param", method = RequestMethod.POST)
    public Response<PageResponse<ClueInitialResponse>> getPageByParam(@RequestBody PageRequest<QueryClueInitialRequest> request) {
        PageResponse<ClueInitial> clueInitialPageResponse = clueInitialService.getPageByParam(ClueInitialAssembler.transformVOToPOPage(request, ClueInitial.class));
        return Response.success(ClueInitialAssembler.transformPOToResponseVoPage(clueInitialPageResponse, ClueInitialResponse.class));
    }

    /**
     * 单条数据插入ClueInitial
     * https://docs.qq.com/sheet/DWVNsbXZybkhmWGJ4?tab=dqbzlc&_t=1642574587912
     * 线索中心接收清洗线索数据----这个接口是外部系统调线索服务的（根据crmId修改或者新增）
     * add by zly
     * 5=CRM清洗新增线索后下发给SCRM(新建和待下发的同步过来)
     * 同一个crmId对应的线索只下发一次
     * @return 插入的条数
     */
    @ApiOperation(value = "线索中心接收清洗线索数据", httpMethod = "POST", response = Response.class, notes = "add")
    @RequestMapping(path = "/access/receiveAfterWash", method = RequestMethod.POST)
    public Response receiveAfterWash(@Valid @RequestBody AddClueInitialAfterWashRequest request) {
        Integer result=clueInitialService.receiveAfterWash(request);
        return Response.success(result);
    }

    /**
     * 单条数据插入ClueInitial
     * https://docs.qq.com/sheet/DWVNsbXZybkhmWGJ4?tab=301qo0&_t=1642574587912
     * add by zly
     * 2022-01-22
     * 3=接收从第三方创建的线索(试驾小程序，经纪人线索，商城)-入库线索源
     * @param request
     * @return 插入的条数
     */
    @ApiOperation(value = "ClueInitial管理-添加信息接口", httpMethod = "POST", response = Response.class, notes = "accessAdd")
    @RequestMapping(path = "/access/add", method = RequestMethod.POST)
    public Response<ThirdCreateClueInitialResponse> accessAdd(@Valid @RequestBody AccessAddClueInitialRequest request) {
        ThirdCreateClueInitialResponse resultRes=clueInitialService.accessAdd(request);
        return Response.success(resultRes);
    }


    /**
     * 定时器执行-同步线索源数据到crm
     * add by zly 2022-01-22
     * 4=CRM接收SCRM中转的线索数据
     * @return
     */
    @ApiOperation(value = "线索源信息定时同步-同步至crm", httpMethod = "GET", response = Response.class, notes = "syncClueInitialToCrm")
    @RequestMapping(value = "/syncClueInitialToCrm", method = RequestMethod.GET)
    public Response<Boolean> syncClueInitialToCrm(@ApiParam(required = false, value = "统计日期,指定到天：yyyy-mm-dd") @RequestParam("sysnTime") String sysnTime){
        Boolean falg = clueInitialService.syncClueInitialToCrm(sysnTime);
        return Response.success(falg);
    }

    @ApiOperation(value = "获取oneId测试", httpMethod = "GET", response = Response.class, notes = "getOneIdByPhone")
    @RequestMapping(value = "/getOneIdByPhone", method = RequestMethod.GET)
    public Response<Object> getOneIdByPhone(@ApiParam(required = true, value = "手机号") @RequestParam("phone") String phone){
       try {
           CenterPhoneRequest centerPhoneRequest = new CenterPhoneRequest();
           centerPhoneRequest.setNation("86");
           centerPhoneRequest.setPhone(phone);
           CenterPhoneResponse oneIdRes = iamIncCenterClient.getOneIdByPhones(centerPhoneRequest, "1");
           log.info("从iam获取oneId,返回信息={}", JSON.toJSONString(oneIdRes));
           return Response.success(oneIdRes);
       }catch (Exception e){
           e.printStackTrace();
       }
       return  Response.error();
    }

    @ApiOperation(value = "线索源入库线索池测试", httpMethod = "GET", response = Response.class, notes = "syncInsertClueTest")
    @RequestMapping(value = "/syncInsertClueTest", method = RequestMethod.GET)
    public Response<Object> syncInsertClueTest(@RequestParam("crmCLueId") String crmCLueId,@RequestParam("leadStatus") Integer leadStatus){
        try {
            clueInitialService.syncInsertClueTest(crmCLueId,leadStatus);
            return Response.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return  Response.error();
    }


}